unit uGxtConfigUtils;

interface

uses
  SysUtils, uDZPersistent;

type
  TUserInfo = class(TDzPersistent)
  private
    FPassword: AnsiString;
    FUserName: AnsiString;
  published
    property UserName: AnsiString read FUserName write fUserName;
    property Password: AnsiString read FPassword write fPassword;
  end;

  TSavedLogins = class(TDzPersistentList)
  public
    constructor Create; reintroduce;
    function GetItem(index: Integer): TUserInfo;
    procedure SetItem(index: Integer; item: TUserInfo);
    function Add: TUserInfo;
    function Extract(index: Integer): TUserInfo;
    procedure Insert(index: Integer; item: TUserInfo);
    function IndexOf(item: TUserInfo): Integer;
    function GetLogin(const UserName: AnsiString): TUserInfo;
    procedure Move(item: TUserInfo; index: Integer);
    property Items[index: Integer]: TUserInfo
      read GetItem write SetItem; default;
  end;

implementation

{ TSavedLogins }

function TSavedLogins.Add: TUserInfo;
begin
  Result := TUserInfo(inherited Add);
end;

constructor TSavedLogins.Create;
begin
  inherited Create(TUserInfo);  
end;

function TSavedLogins.Extract(index: Integer): TUserInfo;
begin
  Result := TUserInfo(inherited Extract(index));
end;

function TSavedLogins.GetItem(index: Integer): TUserInfo;
begin
  Result := TUserInfo(inherited Items[index]);
end;

function TSavedLogins.GetLogin(const UserName: AnsiString): TUserInfo;
var
  i: Integer;
begin
  for i := 0 to Count - 1 do
  begin
    Result := Items[i];
    if SameText(Result.UserName, UserName) then Exit;
  end;
  Result := nil;
end;

function TSavedLogins.IndexOf(item: TUserInfo): Integer;
begin
  Result := inherited IndexOf(item);
end;

procedure TSavedLogins.Insert(index: Integer; item: TUserInfo);
begin
  inherited Insert(index, item);
end;

procedure TSavedLogins.Move(item: TUserInfo; index: Integer);
var
  i: Integer;
begin
  i := IndexOf(item);
  if i <> -1 then Extract(i);
  Insert(index, item);
end;

procedure TSavedLogins.SetItem(index: Integer; item: TUserInfo);
begin
  inherited Items[index] := item;
end;

end.
